package com.kummuna.ui;
import com.kummuna.R;
import com.kummuna.R.layout;
import com.kummuna.R.menu;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;


public class WaterLeakTabMain extends TabActivity implements OnTabChangeListener{


	/** Called when the activity is first created. */
    TabHost tabHost;
    String idFromDetectShortPath;
    String topicFromDetectShortPath;
    String detailFromDetectShortPath;
    String latitudeFromDetectShortPath;
    String longtitudeFromDetectShortPath;
    String distanceFromDetectShortPath;
    String dateFromDetectShortPath;
    String urltitudeFromDetectShortPath;
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.water_leak_tab_main);
         
        // Get TabHost Refference
        tabHost = getTabHost();
        Resources ressources = getResources(); 
         
        // Set TabChangeListener called when tab changed
       // tabHost.setOnTabChangedListener((OnTabChangeListener) this);
     
        TabHost.TabSpec spec;
        Intent intent;
        
      //get intent
        Intent itn=getIntent();
        idFromDetectShortPath = itn.getStringExtra("id");
        topicFromDetectShortPath = itn.getStringExtra("topic");
        detailFromDetectShortPath = itn.getStringExtra("detail");
        latitudeFromDetectShortPath = itn.getStringExtra("latitude");
        longtitudeFromDetectShortPath = itn.getStringExtra("longtitude");
        distanceFromDetectShortPath = itn.getStringExtra("distance");
        dateFromDetectShortPath = itn.getStringExtra("date");
        urltitudeFromDetectShortPath = itn.getStringExtra("url");
   
         /************* TAB1 ************/
        // Create  Intents to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, WaterLeakDetail.class);
        intent.putExtra("id", idFromDetectShortPath);
        intent.putExtra("topic", topicFromDetectShortPath);
        intent.putExtra("detail", detailFromDetectShortPath);
        intent.putExtra("latitude", latitudeFromDetectShortPath);
        intent.putExtra("longtitude", longtitudeFromDetectShortPath);
        intent.putExtra("distance", distanceFromDetectShortPath);
        intent.putExtra("date", dateFromDetectShortPath);
        intent.putExtra("url", urltitudeFromDetectShortPath);
        
        spec = tabHost.newTabSpec("First").setIndicator("", ressources.getDrawable(R.drawable.icon_detail_config))
                      .setContent(intent);
         
        //Add intent to tab
        tabHost.addTab(spec);
   
        /************* TAB2 ************/
        intent = new Intent().setClass(this, UpdateStatus.class);
        spec = tabHost.newTabSpec("Second").setIndicator("", ressources.getDrawable(R.drawable.icon_update_config))
                      .setContent(intent);  
        tabHost.addTab(spec);
   
        /************* TAB3 ************/
        intent = new Intent().setClass(this, HistoryWaterLeak.class);
        intent.putExtra("id", idFromDetectShortPath);
        intent.putExtra("topic", topicFromDetectShortPath);
        intent.putExtra("detail", detailFromDetectShortPath);
        intent.putExtra("latitude", latitudeFromDetectShortPath);
        intent.putExtra("longtitude", longtitudeFromDetectShortPath);
        intent.putExtra("distance", distanceFromDetectShortPath);
        intent.putExtra("date", dateFromDetectShortPath);
        intent.putExtra("url", urltitudeFromDetectShortPath);
        spec = tabHost.newTabSpec("Third").setIndicator("", ressources.getDrawable(R.drawable.icon_history_config))
                      .setContent(intent);
        tabHost.addTab(spec);
        
      //set Windows tab as default (zero based)
        tabHost.setCurrentTab(0);
   
        // Set drawable images to tab
        //tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.icon_update_config);
       // tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.icon_history_config);
           
        // Set Tab1 as Default tab and change image   
      //  tabHost.getTabWidget().setCurrentTab(0);
       // tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.icon_detail_config);
         
   
     }

  @Override
  public void onTabChanged(String tabId) {
       
      /************ Called when tab changed *************/
       
      //********* Check current selected tab and change according images *******/
       
      for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
      {
          if(i==0)
          {
              tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.icon_detail_config);
          }
          else if(i==1)
              tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.icon_update_config);
          else if(i==2)
              tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.icon_history_config);
      }
       
       
      Log.i("tabs", "CurrentTab: "+tabHost.getCurrentTab());
       
  if(tabHost.getCurrentTab()==0)
      tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.icon_detail_config);
  else if(tabHost.getCurrentTab()==1)
      tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.icon_update_config);
  else if(tabHost.getCurrentTab()==2)
      tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.icon_history_config);
  }
  
  @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.water_leak_tab_main, menu);
		return true;
	}
}

